const regeneratorRuntime = require("../../utils/runtime.js")
const {
  _post,
  _get
} = require('../../utils/request.js')
Page({
  data: {
    page: 1,
    limit: 10,
    label: '',
    isFirstLoad: true,
    refreshing: false,
    articleList: {},
    search: '',
    actionId: 0,
    shareing: false,
    actionShow: false,
    hasNextPage: false,
    actions: [{
        name: '分享给朋友',
        openType: 'share'
      },
      {
        name: '复制链接'
      }
    ]

  },

  async onLoad(e) {
    if (e.label) {
      this.setData({
        label: e.label
      })

      wx.setNavigationBarTitle({
        title: e.label + ' - 全栈小组'
      })

      await this.getArticleList()
    }

    if (e.search) {
      this.setData({
        search: e.search
      })

      wx.setNavigationBarTitle({
        title: e.search + ' - 全栈小组'
      })

      await this.getArticleList()
    }
  },

  async getArticleList(isReload) {
    if (this.data.refreshing) {
      return
    }
    this.setData({
      refreshing: true,
      pulling: true
    })
    let lastInfo = []
    if (!this.data.isFirstLoad) {
      wx.showNavigationBarLoading()
    }
    if (isReload) {
      lastInfo = this.data.articleList.list
    } else {
      this.setData({
        page: 1,
      })
    }
    let opt = {
      page: this.data.page,
      limit: this.data.limit,
    }

    if (this.data.label) {
      opt.label = this.data.label
    }

    if (this.data.search) {
      opt.search = this.data.search
    }
    let res = await _get('/article', opt)
    if (res.code === 0) {
      let articleList = res.data
      let hasNextPage = this.data.page < articleList.total_page
      if (lastInfo.length > 0 && articleList.list && articleList.list instanceof Array && articleList.list.page > 1) {
        articleList.list = lastInfo.concat(articleList.list)
      }
      this.setData({
        articleList,
        hasNextPage
      })
    }

    if (this.data.isFirstLoad) {
      this.setData({
        isFirstLoad: false
      })
    }

    this.setData({
      refreshing: false,
      pulling: false
    })
  },

  handleGetShareInfo({
    detail
  }) {
    this.setData({
      actionId: detail.id,
      actionShow: true
    })
  },

  handleActionClose() {
    this.setData({
      actionShow: false,
      actionId: 0
    })
  },

  handleActionSelect({
    detail
  }) {
    if (detail.name === '复制链接') {
      wx.setClipboardData({
        data: `https://aside.fun/archives/${this.data.actionId}`,
        success() {
          wx.showToast({
            title: '内容已复制',
            icon: 'none'
          })
        }
      })
    }
  },

  onShareAppMessage() {
    let shareInfo = {
      title: '别摸鱼了，起来刷题',
      imageUrl: 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1579524369513&di=495f486dd0f5a9ac99fe090b49ac68f4&imgtype=0&src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201803%2F03%2F20180303170608_nJAx8.jpeg',
      path: this.data.actionId ? 'pages/article/article?id=' + this.data.actionId : 'pages/index/index',
      success() {
        if (res.errMsg == 'shareAppMessage:ok') {
          console.log('转发成功')
        }
      },
      fail() {
        if (res.errMsg == 'shareAppMessage:fail cancel') {
          console.log('取消转发')
        } else if (res.errMsg == 'shareAppMessage:fail') {
          console.log('转发失败')
        }
      },
    }

    return shareInfo
  },

  async handleLoadNextPage() {
    if (this.data.hasNextPage) {
      this.setData({
        page: this.data.page + 1
      })
      await this.getArticleList(true)
    }
  },

})